﻿using System;
using System.Text;
using MugenInjection.Infrastructure;
using MugenInjection.Interface;

namespace MugenInjection.Exceptions
{
    /// <summary>
    /// The exception that is thrown when the binding return null result.
    /// </summary>
#if !SILVERLIGHT
#if !NETFX_CORE
    [Serializable]
#endif
#endif
    public class NullableBindingException : BindingException
    {
        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="NullableBindingException"/> class with a specified error message.
        /// </summary>
        /// <param name="bindingContext">The specified <see cref="IBindingContext"/>.</param>
        public NullableBindingException(IBindingContext bindingContext)
            : base(FormatErrorMessage(bindingContext))
        {
        }

        #endregion

        #region Methods

        private static string FormatErrorMessage(IBindingContext bindingContext)
        {
            var sb = new StringBuilder();
            sb.AppendLine(string.Format("Provider return null result for {0}", InjectorUtilsInternal.FormatType(bindingContext.Service)));
            sb.AppendLine("To allow null objects, set the property IInjector.Settings.ThrowErrorForNullableBinding to false.");
            sb.AppendLine("The activation path:");
            sb.AppendLine(InjectorUtilsInternal.GetBuildPath(bindingContext));
            return sb.ToString();
        }

        #endregion
    }
}